package io.renren.modules.basic.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.basic.entity.MtCcProductEntity;
import io.renren.modules.basic.entity.MtCcWarehousPositionRelatEntity;
import io.renren.modules.basic.service.MtCcProductService;
import io.renren.modules.basic.service.MtCcWarehousPositionRelatService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;



/**
 * 
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2019-10-14 10:50:53
 */
@Api(description = "库位详情")
@RestController
@RequestMapping("generator/mtccwarehouspositionrelat")
public class MtCcWarehousPositionRelatController {
    @Autowired
    private MtCcWarehousPositionRelatService mtCcWarehousPositionRelatService;
    @Autowired
    private MtCcProductService mtCcProductService;

    /**
     * 列表
     */
    @ApiOperation(value = "列表",notes="列表")
    @RequestMapping("/list")
    @RequiresPermissions("generator:mtccwarehouspositionrelat:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = mtCcWarehousPositionRelatService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @ApiOperation(value = "信息",notes="信息")
    @RequestMapping("/info/{id}")
    @RequiresPermissions("generator:mtccwarehouspositionrelat:info")
    public R info(@PathVariable("id") Integer id){
		MtCcWarehousPositionRelatEntity mtCcWarehousPositionRelat = mtCcWarehousPositionRelatService.getById(id);

        return R.ok().put("mtccwarehouspositionrelat", mtCcWarehousPositionRelat);
    }

    /**
     * 保存
     */
    @ApiOperation(value = "保存",notes="保存")
    @RequestMapping("/save")
    @RequiresPermissions("generator:mtccwarehouspositionrelat:save")
    public R save(@RequestBody MtCcWarehousPositionRelatEntity mtCcWarehousPositionRelat){
		mtCcWarehousPositionRelatService.save(mtCcWarehousPositionRelat);

        return R.ok();
    }

    /**
     * 修改
     */
    @ApiOperation(value = "修改",notes="修改")
    @RequestMapping("/update")
    @RequiresPermissions("generator:mtccwarehouspositionrelat:update")
    public R update(@RequestBody MtCcWarehousPositionRelatEntity mtCcWarehousPositionRelat){
        String s="";
        String sNull=null;
        mtCcWarehousPositionRelat.setUpdateTime(new Date());
        if (mtCcWarehousPositionRelat.getQuantity()==0){
            mtCcWarehousPositionRelat.setStatus(0);
        }else {
            mtCcWarehousPositionRelat.setStatus(2);
        }
        if (!s.equals(mtCcWarehousPositionRelat.getProductName())&&!sNull.equals(mtCcWarehousPositionRelat.getProductName())){
            MtCcProductEntity product= mtCcProductService.getOne(new QueryWrapper<MtCcProductEntity>().eq("product_name", mtCcWarehousPositionRelat.getProductName()));
            mtCcWarehousPositionRelat.setProductId(product.getId());
        }else {
            mtCcWarehousPositionRelat.setProductId(0);
        }
		mtCcWarehousPositionRelatService.updateById(mtCcWarehousPositionRelat);

        return R.ok();
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除",notes="删除")
    @RequestMapping("/delete")
    @RequiresPermissions("generator:mtccwarehouspositionrelat:delete")
    public R delete(@RequestBody Integer[] ids){
		mtCcWarehousPositionRelatService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
